﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Threading;
using WP8_Client.View;
using System.Windows.Shapes;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media;

namespace WP8_Client
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Konstruktor
        public MainPage()
        {
            InitializeComponent();            
            DataContext = App.DefaultViewModel;

            App.DefaultViewModel.OnUpdateLocation += DefaultViewModel_OnUpdateLocation;
        }

        void DefaultViewModel_OnUpdateLocation(CustomObjects.Location pLocation)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Create a small circle to mark the current location.
                Ellipse _Circle = new Ellipse();
                _Circle.Fill = new SolidColorBrush(Colors.Blue);
                _Circle.Height = 20;
                _Circle.Width = 20;
                _Circle.Opacity = 50;

                // Create a MapOverlay to contain the circle.
                MapOverlay _LocationOverlay = new MapOverlay();
                _LocationOverlay.Content = _Circle;
                _LocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                _LocationOverlay.GeoCoordinate = new System.Device.Location.GeoCoordinate(pLocation.Latitude, pLocation.Longitude);

                // Create a MapLayer to contain the MapOverlay.
                MapLayer myLocationLayer = new MapLayer();
                myLocationLayer.Add(_LocationOverlay);

                // Add the MapLayer to the Map.
                this.cMapControl.Layers.Add(myLocationLayer);
                this.cMapControl.Center = new System.Device.Location.GeoCoordinate(pLocation.Latitude, pLocation.Longitude);
            });
        }

        private void Btn_AddContact_Click_1(object sender, RoutedEventArgs e)
        {
            App.DefaultViewModel.AddContact();
        }

        // Daten für die ViewModel-Elemente laden
        //protected override void OnNavigatedTo(NavigationEventArgs e)
        //{
        //    if (!App.ViewModel.IsDataLoaded)
        //    {
        //        App.ViewModel.LoadData();
        //    }
        //}        
    }
}